#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "hal_uart.h"


void uart_init(int baud,int buffer_size)
{
    uart_config_t uart_config = {
        .baud_rate = baud,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };

    uart_param_config(UART_USER_NUM, &uart_config);
    uart_set_pin(UART_USER_NUM,UART_TX_PIN_NUM , UART_RX_PIN_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    uart_driver_install(UART_USER_NUM, buffer_size * 2, 0, 0, NULL, 0);
}

int uart_read(void *data,int len)
{
    int length = 0;
    ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_USER_NUM, (size_t*)&length));
    length = (length < len)?(length):(len);
    length = uart_read_bytes(UART_USER_NUM, data, length, 10);
    return length;  
}

void uart_write(void *data,int len)
{
     uart_write_bytes(UART_USER_NUM, (const char *) data, len);
}
  